package cc.server.extension.controller;

import cc.server.extension.common.R;
import cc.server.extension.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cc.server.extension.entity.Category;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    private final CategoryService categoryService;

    @Autowired
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 获取分类列表
     * */
//    @GetMapping("/page")
//    public R<Page<Category>> page(int page, int pageSize) {
//        // 构造分页构造器
//        Page<Category> pageInfo = new Page<>(page, pageSize);
//        // 构造条件构造器
//        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//        // 添加排序条件
//        queryWrapper.orderByDesc(Category::getSort);
//        // 执行查询
//        categoryService.page(pageInfo, queryWrapper);
//        return R.success(pageInfo);
//    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        // 构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        // 添加条件
        queryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        // 添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        // 执行查询
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
